<template>
  <div class="component-distribution-user">
    <!-- 搜索栏 -->
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item :label="$t('distributionMsg.nickName') + ':'">
            <el-input
              v-model="searchForm.nickName"
              style="min-width: 260px;"
              type="text"
              clearable
              :placeholder="$t('distributionMsg.nickName')"
            />
          </el-form-item>
          <el-form-item :label="$t('distribUserWallet.mobileTelNumber') + ':'">
            <el-input
              v-model="searchForm.userMobile"
              style="min-width: 260px;"
              type="text"
              clearable
              :placeholder="$t('distribUserWallet.mobileTelNumber')"
            />
          </el-form-item>
          <!--状态-->
          <el-form-item :label="$t('distribUserWallet.status') + ':'">
            <el-select
              v-model="searchForm.state"
              :placeholder="$t('distribUserWallet.status')"
              clearable
            >
              <el-option
                :label="$t('distribUserWallet.perBan')"
                :value="-1"
              />
              <el-option
                :label="$t('publics.normal')"
                :value="1"
              />
              <el-option
                :label="$t('distribUserWallet.ban')"
                :value="2"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="onResetSearch('searchForm')"
            >
              {{ $t('product.reset') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <div class="main-container">
      <!-- 表格 -->
      <div class="table-con distribution-achievement-basic-table">
        <el-table
          ref="achievementBasicTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
          @sort-change="sortChange"
        >
          <!-- 分销员昵称 -->
          <el-table-column
            width="200"
            prop="nickName"
            :label="$t('distribUserWallet.distributorNickname')"
          >
            <template #default="scope">
              {{ scope.row.nickName }}
            </template>
          </el-table-column>
          <!-- 分销员手机号 -->
          <el-table-column
            width="150"
            prop="userMobile"
            :label="$t('distribUserWallet.mobileTelNumber')"
          >
            <template #default="scope">
              {{ scope.row.userMobile }}
            </template>
          </el-table-column>

          <!--状态-->
          <el-table-column
            min-width="100"
            prop="state"
            :label="$t('distribUserWallet.status')"
          >
            <template #default="scope">
              <div
                v-if="scope.row.state === -1"
                class="tag-text"
              >
                {{ $t("distribUserWallet.perBan") }}
              </div>
              <div
                v-if="scope.row.state === 1"
                class="tag-text"
              >
                {{ $t("publics.normal") }}
              </div>
              <div
                v-if="scope.row.state === 2"
                class="tag-text"
              >
                {{ $t("distribUserWallet.ban") }}
              </div>
            </template>
          </el-table-column>

          <!-- 累计收益 -->
          <el-table-column
            prop="addupAmount"
            :label="$t('distribution.accumulateIncome')"
            min-width="120"
          >
            <template #default="scope">
              <div
                v-if="
                  scope.row.distributionUserAchievementDataDto.distributionUserWallet
                    .addupAmount === null
                "
              >
                0
              </div>
              <div v-else>
                {{
                  scope.row.distributionUserAchievementDataDto.distributionUserWallet
                    .addupAmount
                }}
              </div>
            </template>
          </el-table-column>
          <!-- 直推奖励 -->
          <el-table-column
            min-width="100"
            prop="aGenerationCommission"
            :label="$t('distribIncome.generationCommission')"
            sortable="custom"
          >
            <template #default="scope">
              <span>
                {{
                  scope.row.distributionUserAchievementDataDto.distributionUserIncome
                    .agenerationCommission
                }}
              </span>
            </template>
          </el-table-column>
          <!-- 间推奖励 -->
          <el-table-column
            min-width="100"
            prop="secondGenerationCommission"
            :label="$t('distribIncome.secondGenerationCommission')"
            sortable="custom"
          >
            <template #default="scope">
              <span>
                {{
                  scope.row.distributionUserAchievementDataDto.distributionUserIncome
                    .secondGenerationCommission
                }}
              </span>
            </template>
          </el-table-column>
          <!-- 待结算金额 -->
          <el-table-column
            min-width="120"
            prop="unsettledAmount"
            :label="$t('distribUserWallet.waitTosettled')"
            sortable="custom"
          >
            <template #default="scope">
              <span>
                {{
                  scope.row.distributionUserAchievementDataDto.distributionUserWallet
                    .unsettledAmount
                }}
              </span>
            </template>
          </el-table-column>
          <!-- 可提现金额 -->
          <el-table-column
            min-width="120"
            prop="settledAmount"
            :label="$t('distribUserWallet.withdrawalAmount')"
            sortable="custom"
          >
            <template #default="scope">
              <span>
                {{
                  scope.row.distributionUserAchievementDataDto.distributionUserWallet
                    .settledAmount
                }}
              </span>
            </template>
          </el-table-column>
          <!-- 已失效金额 -->
          <el-table-column
            min-width="120"
            prop="invalidAmount"
            :label="$t('distribUserWallet.invalidAmount')"
            sortable="custom"
          >
            <template #default="scope">
              <span>
                {{
                  scope.row.distributionUserAchievementDataDto.distributionUserWallet
                    .invalidAmount
                }}
              </span>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页 -->
      <el-pagination
        v-if="dataList.length"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="onPageSizeChange"
        @current-change="onPageChange"
      />
    </div>
  </div>
</template>

<script setup>
onMounted(() => {
  getDataList()
})

/**
 * 点击查询
 * @param newData
 */
const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}

const sortParam = ref(7)
const sortType = ref(2)
/**
 * 对应6种排序
 * @param data
 */
const sortChange = (data) => {
  if (data.order === null) {
    // 当没有使用列进行排序时，恢复默认排序s
    sortParam.value = 7
    sortType.value = 2
    getDataList(page)
    return
  }
  // 排序字段 0无 1一代佣金 2二代佣金 3邀请奖励 4待结算金额 5可提现金额 6已失效金额
  switch (data.prop) {
    case 'aGenerationCommission': sortParam.value = 1
      break
    case 'secondGenerationCommission': sortParam.value = 2
      break
    case 'invitationRewards': sortParam.value = 3
      break
    case 'unsettledAmount': sortParam.value = 4
      break
    case 'settledAmount': sortParam.value = 5
      break
    case 'invalidAmount': sortParam.value = 6
      break
    default: sortParam.value = 7
      break
  }
  // 排序类型 0无 1 正序 2倒序
  switch (data.order) {
    case 'descending': sortType.value = 2
      break
    case 'ascending': sortType.value = 1
      break
    default: sortType.value = 2
      break
  }
  getDataList(page)
}

let tempSearchForm = null // 保存上次点击查询的请求条件
let theParams = null // 保存上次点击查询的请求条件
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const searchForm = ref({})
const dataList = ref([])
const dataListLoading = ref(false)
/**
 * 获取数据列表
 * @param pageParam
 * @param newData
 */
const getDataList = (pageParam, newData = false) => {
  dataListLoading.value = true
  if (newData || !tempSearchForm) {
    theParams = JSON.parse(JSON.stringify(searchForm.value))
  }
  tempSearchForm = {
    current: pageParam ? pageParam.currentPage : page.currentPage,
    size: pageParam ? pageParam.pageSize : page.pageSize,
    sortType: sortType.value,
    sortParam: sortParam.value
  }

  http({
    url: http.adornUrl('/distribution/distributionUser/page/achievement'),
    method: 'get',
    params: http.adornParams(Object.assign(tempSearchForm, theParams))
  })
    .then(({ data }) => {
      page.total = data.total
      page.pageSize = data.size
      page.currentPage = data.current
      dataList.value = data.records
      dataListLoading.value = false
    })
}

const searchFormRef = ref(null)
/**
 * 重置表单
 */
const onResetSearch = () => {
  searchFormRef.value?.resetFields()
  searchForm.value = {}
}

const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList(page)
}
const onPageChange = (val) => {
  page.currentPage = val
  getDataList(page)
}

</script>
<style lang="scss" scoped>
:deep(.search-bar .input-row .el-form-item__content .el-input) {
  width: 100%;
}
</style>
